Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add syntax to create a XorT from an F[A] and from a Xor. #1155

Merged
merged 1 commit into from
Jun 30, 2016

Conversation

peterneyens
Copy link
Collaborator

@peterneyens peterneyens commented Jun 20, 2016

I have added syntax to create a XorT from a

The last one is a little bit difficult:

  • If I define it on Xor I need to work around the covariance of A and B in Xor versus the invariance in XorT, so you would need to write xor.toXorT[F, A, B].
  • I have also defined toXorT (currently toXorT2 in syntax.xor) as a syntax function on Xor so you can write xor.toXorT[F]. But it's probably not expected that there are syntax functions for a data type defined in cats itself (at least I couldn't think of / find an earlier precedent).

I prefer the syntax function because it is nicer/easier to use, but it would probably be more difficult to find, since it won't be in the Xor scaladoc. WDYT ?

@@ -228,7 +229,7 @@ class XorTests extends CatsSuite {
}
}

test("combine is right iff both operands are right") {
test("combine is right if both operands are right") {
forAll { (x: Int Xor String, y: Int Xor String) =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm, I wonder why you changed this. This test seems testing iff to me

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, thought it was a typo 😉

@codecov-io
Copy link

codecov-io commented Jun 20, 2016

Current coverage is 88.78%

Merging #1155 into master will decrease coverage by 0.01%

@@             master      #1155   diff @@
==========================================
  Files           232        233     +1   
  Lines          3073       3077     +4   
  Methods        3021       3024     +3   
  Messages          0          0          
  Branches         49         50     +1   
==========================================
+ Hits           2729       2732     +3   
- Misses          344        345     +1   
  Partials          0          0          

Sunburst

Powered by Codecov. Last updated by ed21654...258fac6

* }}}
*/
def toXorT[F[_], L, R](implicit F: Applicative[F], evL: A <:< L, evR: B <:< R): XorT[F, L, R] =
XorT(F.pure(bimap(evL, evR)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm if toXorT has to respecify all type parameters I am not sure I will favor it over fromXor

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, that is what I was thinking. What do you think about adding it as a syntax method (see syntax.xor.toXorT2) ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I wasn't thinking about the variance on Xor when I proposed this previously. The syntax approach doesn't look bad, but I want to ponder our options a bit.

@kailuowang
Copy link
Contributor

I vote for the toXorT2 in syntax. The one on data class itself is hard to use and doesn't feel right to me. The users I know are used to find methods in syntax so discoverability doesn't seem an issue to me.

@ceedubs
Copy link
Contributor

ceedubs commented Jun 24, 2016

For anyone who has asked about what the downsides of variance are, this is an example :)

What if we were to put something like this in the Xor companion object?

  final implicit class XorOps[A, B](val value: A Xor B) extends AnyVal {
    def toXorT[F[_]:Applicative]: XorT[F, A, B] = XorT.fromXor(value)
  }

If we do that then people should automatically pick up the syntax on their Xor instances and therefore it will probably show up in their IDE's autocomplete. As you've mentioned, people probably won't expect to need an import to add this sort of method onto Xor, which is why I'm inclined to go down this route as opposed to the normal syntax route; I would view this not being directly on Xor as an implementation detail and not something that people should have to think about.

We could also link to XorOps within the Xor scaladoc with a note like "some additional Xor methods can be found here" and maybe a brief explanation of why they are separate.

What do you think?

@peterneyens peterneyens force-pushed the creating-xort-from-xor-or-fa branch 2 times, most recently from dc8f34a to 374fbeb Compare June 25, 2016 11:41
new XorTOps[U.M, U.A](U.subst(fa))(U.TC)
}

final class XorTOps[F[_]: Functor, A](val fa: F[A]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we call this XorTFunctorOps or something? We could conceivably want to add XorT-related syntax onto types other than F[A]. I think it should still be mixed in via XorTSyntax though.

@kailuowang
Copy link
Contributor

Many thanks! 👍

implicit def catsSyntaxXorT[F[_]: Functor, A](fa: F[A]): XorTFunctorOps[F, A] = new XorTFunctorOps(fa)
}

trait XorTSyntax1 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably want to make this private (as per #1160).

@peterneyens
Copy link
Collaborator Author

Thanks for the comments @ceedubs and @kailuowang. Even a simple PR like this looks a lot nicer after improving the little issues you pointed out.

@ceedubs
Copy link
Contributor

ceedubs commented Jun 30, 2016

👍 excellent. Thank you @peterneyens!

@ceedubs ceedubs merged commit 5100ab5 into typelevel:master Jun 30, 2016
@peterneyens peterneyens deleted the creating-xort-from-xor-or-fa branch July 18, 2016 12:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants